<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/jApiController.php';

class GeoModel extends jApiController{
	public function def(){
	  return 'welcome Geo jApi!';
	}
	
	public static function getView(){
	  include_once $_SERVER['DOCUMENT_ROOT'].'/lib/GeoView.php';

	  return GeoView::inst();
	}
	
	public function getCountries($prm=array()){
   	if(!$this->checkAuth()) return false;

	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;
	
	  $sql_prm=array();
	  $sqlBase="SELECT ctr_id,ctr_sysname,ctr_name,ctr_lang,ctr_cur,ctr_geometry,ctr_managed FROM country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln)";
	  
		$sql=$sqlBase.' ORDER BY ctr_name ';
    $sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);

	  $res= $this->getData($sql,$sql_prm);
		$total=$this->getData("SELECT count(ctr_id) ttl FROM country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln)",false,true);
		
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$res
		);
	}
	public function getCountry($id=false){
	  if(!$this->checkAuth()) return false;
	  
	  if( empty($id) || !$this->maskID($id,'country') )
			return $this->error('get country: ID expected');

	  return $this->getData(
			"SELECT ctr_id,ctr_sysname,ctr_name,ctr_lang,ctr_cur,ctr_geometry,ctr_managed FROM country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln) WHERE ctr_id=:id"
			,array('id'=>$id)
			,true
		);
	}
	
	public function getRegions($id=false,$prm=array()){
	  if(!$this->checkAuth()) return false;
	  
	  if( empty($id) || !$this->maskID($id,'country') )	return $this->error('get regions: country ID expected');
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

    //Построение запроса
	  $sql="SELECT reg_id,reg_name FROM region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln) WHERE reg_key_ctr=:id";
	  $sql_prm=array( 'id'=>$id );
	  
    $sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);
		
		$res=$this->getData($sql,$sql_prm,false );
		$total=$this->getData("SELECT count(reg_id) ttl FROM region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln) WHERE reg_key_ctr=:id",array( 'id'=>$id ),true );
		
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$res
		);
		//return 
	}
	
	public function getRegion($id=false){
	  if(!$this->checkAuth()) return false;

	  if( empty($id) || !$this->maskID($id,'region') )	return $this->error('getRegion:region ID expected');
		return $this->getData(
			"SELECT ctr_id,ctr_sysname,ctr_name,reg_id,reg_sysname,reg_name,reg_geometry FROM `region`  LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln),`country` LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln) WHERE ctr_id=reg_key_ctr AND reg_id=:id"
			,array('id'=>$id), true
		);
	}
	
	public function getCities($id=false,$prm=array()){
	  if(!$this->checkAuth()) return false;
	  if( empty($id) )	return $this->error('get cities: region or country ID expected');
	  if( !$this->maskID($id,'country|region') )	return $this->error('get cities: region or country ID expected');
	  
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;
    
    //Построение запроса
	  $sqlBase="SELECT cit_id,cit_sysname,cit_name,cit_key_reg,cit_key_ctr  FROM city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln) WHERE";
	  
	  if( $ss=='rg' )	$where=' cit_key_reg=:id';
		else $where=' cit_key_ctr=:id';
		$sql=$sqlBase.$where;
		
	  $sql_prm=array( 'id'=>$id );

		$sql.=' ORDER BY cit_name ';
    $sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);
		
		$cities=$this->getData($sql,$sql_prm,false );
		
		$sql=$sqlBase="SELECT count(cit_id) ttl FROM city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln) WHERE $where";
		$total=$this->getData($sql,array( 'id'=>$id ),true );
		
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$cities
		);
	}
	
	public function getCity($id=false){
	  if(!$this->checkAuth()) return false;
	  if( empty($id) || !$this->maskID($id,'city') )	return $this->error('get city: ID expected');
	  $sql="SELECT
	          ctr_id, ctr_sysname, ctr_name,
	          reg_id, reg_sysname, reg_name,
						cit_id,	cit_sysname, cit_name
					FROM
						city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln),
						region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln),
						country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln)
					WHERE
						cit_id=:id AND reg_id=cit_key_reg AND ctr_id=reg_key_ctr";
		return $this->getData($sql,array('id'=>$id), true );
	}

	public function getUrbans($id=false, $prm=array()){
	  if(!$this->checkAuth()) return false;

	  if(empty($id) || !$this->maskID($id,'country|region|city') )
	    return $this->error('get urbanareas: country, region or city ID expected');
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

	  $sqlBase="SELECT ua_id,ua_sysname,ua_name,ua_key_ctr,ua_key_reg,ua_key_cit FROM urbanarea LEFT JOIN urbanarea_name ON(ua_id=ua_key AND ua_ln=:ln) WHERE ";
  	switch( mb_substr($id, 0, 2, 'utf8') ){
  	  case 'ct':  $add=' ua_key_cit=:id'; break;
  	  case 'rg':  $add=' ua_key_reg=:id'; break;
  	  default:
				if( mb_strlen($id, 'utf8')==2 ) $add=' ua_key_ctr=:id';
				else	return $this->error('get urbanareas: country, region or city ID expected');
		}
		$sql=$sqlBase.$add;
		$sql_prm=array('id'=>$id);
		
    $sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);

		$result=$this->getData($sql,$sql_prm,false );
		
		$sql="SELECT count(ua_id) ttl FROM urbanarea LEFT JOIN urbanarea_name ON(ua_id=ua_key AND ua_ln=:ln) WHERE ".$add;
		$total=$this->getData($sql,array('id'=>$id),true );
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$result
		);
	}
	
	public function getUrban($id=false){
	  if(!$this->checkAuth()) return false;
	  if( empty($id) || !$this->maskID($id,'urbanarea') )
			return $this->error('get urbanarea: ID expected');
	  $sql="SELECT
	          ctr_id, ctr_sysname, ctr_name,
	          reg_id, reg_sysname, reg_name,
      			cit_id,	cit_sysname, cit_name,
      			ua_id,	ua_sysname,	 ua_name
					FROM
						city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln),
						region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln),
						country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln),
						urbanarea LEFT JOIN urbanarea_name ON(ua_id=ua_key AND ua_ln=:ln)
					WHERE
						ua_id=:id AND reg_id=cit_key_reg AND ctr_id=reg_key_ctr AND ua_key_cit=cit_id";
		$rs=$this->getData($sql,array('id'=>$id),true );

		return $rs;
	}
	
	public function getPoints($id=false,$prm=array()){
	  if(!$this->checkAuth()) return false;

	  if(empty($id) || !$this->maskID($id,'country|region|city|urbanarea') )
			return $this->error('get points: country,region,city or urbanarea ID expected');

	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

	  
	  $sqlBase="SELECT p_id,p_key_net,p_sysname,p_name  FROM point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln) WHERE ";
	  //p_key_reg=:reg
  	switch( mb_substr($id, 0, 2, 'utf8') ){
  	  case 'ct':  $add=' p_key_cit=:id'; break;
  	  case 'rg':  $add=' p_key_reg=:id'; break;
  	  case 'ub':  $add=' p_key_ua=:id'; break;
  	  case 'ad':  $add=' p_key_addr=:id'; break;
  	  default:
				if( mb_strlen($id, 'utf8')==2 ) $add=' p_key_ctr=:id';
				else	return $this->error('get points: expected ID of country,region,city,urbanarea');
		}
		$sql=$sqlBase.$add;
		$sql_prm=array('id'=>$id);

		$sql.=' LIMIT :from,:limit';
		$sql_prm['from']	 = array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit'] = array(	$limit,PDO::PARAM_INT	);
//echo $sql.'<pre>',print_r( $sql_prm, true),'</pre>';
		$result= $this->getData( $sql , $sql_prm,false );
		
		$sql="SELECT count(p_id) ttl  FROM point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln) WHERE ".$add;
		$total=$this->getData($sql,array('id'=>$id),true );
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$result
		);
	}
	
	public function getPoint($id=fale){
	  if(!$this->checkAuth()) return false;

	  if(empty($id) || !$this->maskID($id,'point') )
			return $this->error('get point: ID expected');
	  $sql="SELECT
						p_id,p_key_addr,p_lat,p_lng,p_sysname,p_name,p_data_provider,
						ctr_id,ctr_sysname,ctr_name,
						reg_id,reg_sysname,reg_name,
						cit_id,cit_sysname,cit_name,
						ua_id,ua_sysname,ua_name,
						net_id,net_sysname,net_name
					FROM
						point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln),
						country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln),
						region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln),
						city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln),
						urbanarea LEFT JOIN urbanarea_name ON(ua_id=ua_key AND ua_ln=:ln),
						network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln)
					WHERE
						p_id=:id AND
						p_key_ctr=ctr_id AND
						p_key_reg=reg_id AND
						p_key_cit=cit_id AND
						p_key_ua=ua_id AND
						p_key_net=net_id";
		return $this->getData($sql,array('id'=>$id), true );
	}
/*
	public function getAddrs($id=false, $prm=array()){
	  if( empty($id) ) 
	  	return $this->error('get addresses: country, region, city or aurbanarea ID expected');
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

	  $sql="SELECT DISTINCT p_key_ctr,p_key_reg,p_key_cit,p_key_ua,p_name
		 FROM point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln) WHERE ";
  	switch( mb_substr($id, 0, 2, 'utf8') ){
  	  case 'rg':  $add=' addr_key_reg=:id'; break;
  	  case 'ct':  $add=' addr_key_cit=:id'; break;
  	  case 'ub':  $add=' addr_key_ua=:id'; break;
  	  default:
				if( mb_strlen($id, 'utf8')==2 ) $add=' addr_key_ctr=:id';
				else
				  return $this->error('get addresses: country, region, city or aurbanarea ID expected');
		}
		$sql.=$add;
		$sql_prm=array('id'=>$id);

    $sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);
		//echo $sql.'<pre>',print_r( $sql_prm, true),'</pre>';
		return $this->getData($sql,$sql_prm );
	}
	
	public function getAddr($id=false){
	  if( empty($id) ) throw_error('get address: ID expected',400);
	  $sql="SELECT
      			addr_id,addr_sysname,addr_lat,addr_lng,addr_name,
						ctr_id,ctr_sysname,ctr_name,
						reg_id,reg_sysname,reg_name,
						cit_id,cit_sysname,cit_name,
						ua_id,ua_sysname,ua_name
					FROM
						addr LEFT JOIN addr_name ON(addr_id=addr_key AND addr_ln=:ln),
						country LEFT JOIN country_name ON(ctr_id=ctr_key AND ctr_ln=:ln),
						region LEFT JOIN region_name ON(reg_id=reg_key AND reg_ln=:ln),
						city LEFT JOIN city_name ON(cit_id=cit_key AND cit_ln=:ln),
						urbanarea LEFT JOIN urbanarea_name ON(ua_id=ua_key AND ua_ln=:ln)
					WHERE
						addr_id=:id AND
						addr_key_ctr=ctr_id AND
						addr_key_reg=reg_id AND
						addr_key_cit=cit_id AND
						addr_key_ua=ua_id";
		return $this->getData($sql,array('id'=>$id), true );
	}
*/
	
	public function getNetworks($id=false,$prm=array()){
	  if(!$this->checkAuth()) return false;

	  if(empty($id) || !$this->maskID($id,'country|region|city|urbanarea') )
	  	return $this->error('get networks: expected ID of country,region,city or urbanarea');

	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

	  $sqlBase="SELECT DISTINCT net_id,net_sysname,net_name FROM
		          network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln),
							point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln)
						 WHERE net_id=p_key_net ";
  	switch( mb_substr($id, 0, 2, 'utf8') ){
  	  case 'ct':  $add='AND p_key_cit=:id'; break;
  	  case 'rg':  $add='AND p_key_reg=:id'; break;
  	  case 'ub':  $add='AND p_key_ua=:id'; break;
  	  default:
				if( mb_strlen($id, 'utf8')==2 ) $add='AND p_key_ctr=:id';
				else return $this->error('get networks: expected ID of country,region,city or urbanarea');
		}
		$sql=$sqlBase.' '.$add;
	  $prm=array('id'=>$id);
		$sql.=' LIMIT :from,:limit';
		$prm['from']	 = array(	($page*$limit),PDO::PARAM_INT	);
		$prm['limit'] = array(	$limit,PDO::PARAM_INT	);
		
		$result=$this->getData($sql,$prm,false );
		
		$sql="SELECT count(net_id) ttl FROM
						point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln),
						network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln) WHERE net_id=p_key_net ".$add.' GROUP BY net_id';

		$total=$this->getData($sql,array('id'=>$id),true );
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$result
		);
	}
	
	public function getNetwork($id=false){
	  if(!$this->checkAuth()) return $this->error('Auth required');

	  if(empty($id) || !$this->maskID($id,'network') )
			return $this->error('get network: ID expected');

	  $sql="SELECT
	          net_id,net_sysname,net_name,net_forward
					FROM
						network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln)
					WHERE net_id=:id";

		$rs=$this->getData($sql,array('id'=>$id), true );
		if( !empty($rs['net_forward']) )
		  $rs=$this->getNetwork( $rs['net_forward'],false,false );
		return $rs;
	}
	
	public function findNetwork($name){
	  if(!$this->checkAuth()) return false;

	  if( empty($name) )
	    return $this->error('get network: network name expected');
	    
	  $sql="SELECT
	          net_id,net_sysname,net_name,net_forward
					FROM
						network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln)
					WHERE net_ch1=:ch1 AND net_name=:name LIMIT 0,1";
					
		$name=netNormalName($name);
		$ch1=mb_substr($name,0, 1,'utf8');
		
		$rs=$this->getData($sql,array('ch1'=>$ch1,'name'=>$name), true );
		if( !empty($rs['net_forward']) )
		  $rs=$this->getNetwork( $rs['net_forward'] );
    return $rs;
	}

	public function getPointsNear($lat,$lng,$radius=6,$prm=array()){
	  if(!$this->checkAuth()) return false;

	  if(empty($radius)) $radius=6;
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;
	  /*
			set @mylat =50.4325814;
			set @mylng =30.4635537;
			set @dist	 =6;
			set @dlng1 = @mylng-@dist/abs(cos(radians(@mylat))*69);
			set @dlng2 = @mylng+@dist/abs(cos(radians(@mylat))*69);
			set @dlat1 = @mylat-(@dist/69);
			set @dlat2 = @mylat+(@dist/69);

			SELECT *,
			3956 * 2 * ASIN(SQRT( POWER(SIN((@mylat - s_lat) *  pi()/180 / 2), 2) +COS(@mylat * pi()/180) * COS(s_lat * pi()/180) * POWER(SIN((@mylng -s_lng) * pi()/180 / 2), 2) ))
			as distance
			FROM sallers
			WHERE s_lng between @dlng1 and @dlng2 and s_lat BETWEEN @dlat1 and @dlat2;
		*/
		//
		$prm=array(
      'mylat' => $lat,
      'mylng' => $lng,
      'dist'  => $radius,
		);
    $prm['dlng1'] = $prm['mylng']-$prm['dist']/abs(cos(deg2rad($prm['mylat']))*69);
		$prm['dlng2'] = $prm['mylng']+$prm['dist']/abs(cos(deg2rad($prm['mylat']))*69);
		$prm['dlat1'] = $prm['mylat']-($prm['dist']/69);
		$prm['dlat2'] = $prm['mylat']+($prm['dist']/69);
		$sqlBase="SELECT {fields}
					FROM point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln)
					WHERE
            p_lng BETWEEN :dlng1 AND :dlng2 AND
						p_lat BETWEEN :dlat1 AND :dlat2 ";
		$fld="p_id, p_sysname, p_name, p_lat, p_lng, p_key_addr,p_key_net,p_key_ctr,p_key_reg,p_key_cit,p_key_ua,
						(
							3956 * 2 * ASIN( SQRT( POWER( SIN( (:mylat - p_lat) *  pi()/180 / 2 ), 2 ) + COS( :mylat * pi()/180 ) * COS( p_lat * pi()/180 ) * POWER( SIN( (:mylng -p_lng) * pi()/180 / 2 ), 2 ) ) )
						) p_distance";
		$ord=" ORDER BY p_distance,p_name,p_sysname";
						
		$sql=str_replace('{fields}',$fld,$sqlBase).$ord.' LIMIT :from,:limit';
		$sql_prm=$prm;
		$sql_prm['from']	 = array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit'] = array(	$limit,PDO::PARAM_INT	);

		$result=$this->getData($sql,$sql_prm,false );
		
		$sql=str_replace('{fields}','count(p_id) ttl',$sqlBase);
		$sql_prm=array(
		  'dlng1'=>$prm['dlng1'],
		  'dlng2'=>$prm['dlng2'],
		  'dlat1'=>$prm['dlat1'],
		  'dlat2'=>$prm['dlat2'],
		);
		//echo $sql.'<pre class="debug">'.print_r ( $sql_prm ,true).'</pre>';
		$total=$this->getData($sql,$sql_prm,true );
		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$result
		);
	}
	public function getNetworksNear($lat,$lng,$radius=6,$prm=array()){
	  if(!$this->checkAuth()) return false;

	  if(empty($radius)) $radius=6;
	  //Получение GET параметров
	  $limit=DEF_LISTRES_LIMIT;
	  if($prm['page']=='all') $limit=DEF_LISTRES_ALL_LIMIT;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;
    
		$prm=array(
      'mylat' => $lat,
      'mylng' => $lng,
      'dist'  => $radius,
		);
    $prm['dlng1'] = $prm['mylng']-$prm['dist']/abs(cos(deg2rad($prm['mylat']))*69);
		$prm['dlng2'] = $prm['mylng']+$prm['dist']/abs(cos(deg2rad($prm['mylat']))*69);
		$prm['dlat1'] = $prm['mylat']-($prm['dist']/69);
		$prm['dlat2'] = $prm['mylat']+($prm['dist']/69);
		$sql="SELECT net_id, net_sysname,net_name, p_id, p_sysname,p_name, p_lat, p_lng, (
						3956 * 2 * ASIN( SQRT( POWER( SIN( (:mylat - p_lat) *  pi()/180 / 2 ), 2 ) + COS( :mylat * pi()/180 ) * COS( p_lat * pi()/180 ) * POWER( SIN( (:mylng -p_lng) * pi()/180 / 2 ), 2 ) ) )
					) n_distance
					FROM
						point LEFT JOIN point_name ON(p_id=p_key AND p_ln=:ln),
						network LEFT JOIN network_name ON(net_id=net_key AND net_ln=:ln)
					WHERE
					  net_id=p_key_net AND
            p_lng BETWEEN :dlng1 AND :dlng2 AND
						p_lat BETWEEN :dlat1 AND :dlat2
					ORDER BY n_distance,net_name";
		$sql.=' LIMIT :from,:limit';
		$prm['from']	 = array(	($page*$limit),PDO::PARAM_INT	);
		$prm['limit'] = array(	$limit,PDO::PARAM_INT	);
		
		return $this->getData($sql,$prm,false );
	}
}
?>